import {Component, OnInit, Input} from "@angular/core";
import "datatables";
import {FormBuilder, FormGroup} from "@angular/forms";

import {CIModifyInfo} from "./company-info-modify.class";
import {CIModifyService} from "./company-info-modify.service";
import {entertypes} from '../../../../constants/entertypes';
import {provinces} from '../../../../constants/provinces';



@Component({
  selector: 'cim-modify',
  templateUrl: 'company-info-modify.component.html',
  styleUrls: ['company-info-modify.component.scss'],
  providers: [CIModifyService]
})
export class CompInfoModifyComponent implements OnInit {
  @Input() companyOrigininfoUrl = '/water/enterpriseSub/getOrInfoOfEnterprise';
  @Input() companyinfoUrl = '/water/enterpriseSub/getInfoOfEnterprise';

  private _ciModifyUrl = '/water/enterpriseSub/updateEnterprise';

  private ciModifyForm: FormGroup;

  private ciModifyInfo: CIModifyInfo = new CIModifyInfo();
  private ciModifyNewInfo: CIModifyInfo = new CIModifyInfo();
  private fileInfo: any = {};

  private isModifying: boolean = false;

  private _errorMsg: string;

  private fileUploadUrl = '/platform/doc/upload';
  private disableUpload: boolean = false;
  private appLetter: string = 'appLetter';
  private constructionCert: string = 'constructionCert';
  private securityCert: string = 'securityCert';
  private businessLicense: string = 'businessLicense';
  private sizeLimit = 2097152;

  private entertypes = entertypes;
  private provinces = provinces;

  private dateInfo: any = {};


  private validationMessages = {
    //TODO 补全校验规则
  };

  private formErrors = {
    'name': '',
    'address': '',
    'email': ''
  };


  // constructor(private _fb: FormBuilder) {
  // }
  constructor(private _fb: FormBuilder, private _service: CIModifyService) {
  }

  ngOnInit() {
    this.getUserOriginInfo();
    this.getUserInfo();
    this.buildForm();
  }

  buildForm(): void {

    this.ciModifyForm = this._fb.group(
      {
        'enterpriseSub_address': [this.ciModifyNewInfo.enterpriseinfo.address],
        'enterpriseSub_registerarea': [this.ciModifyNewInfo.enterpriseinfo.registerarea],
        'enterpriseSub_registeredcapital': [this.ciModifyNewInfo.enterpriseinfo.registeredcapital],
        'enterpriseSub_website': [this.ciModifyNewInfo.enterpriseinfo.website],
        'enterpriseSub_postcode': [this.ciModifyNewInfo.enterpriseinfo.postcode],
        'enterpriseSub_permitid': [this.ciModifyNewInfo.enterpriseinfo.permitid],
        'enterpriseSub_name': [this.ciModifyNewInfo.enterpriseinfo.name],
        'enterpriseSub_email': [this.ciModifyNewInfo.enterpriseinfo.email],
        'enterpriseSub_contact': [this.ciModifyNewInfo.enterpriseinfo.contact],
        'enterpriseSub_contacttel': [this.ciModifyNewInfo.enterpriseinfo.contacttel],
        'enterpriseSub_certificateid': [this.ciModifyNewInfo.enterpriseinfo.certificateid],
        'enterpriseSub_entertype': [this.ciModifyNewInfo.enterpriseinfo.entertype],
        'enterpriseSub_establishtime': [this.ciModifyNewInfo.enterpriseinfo.establishtime],
        'enterpriseSub_industry': [this.ciModifyNewInfo.enterpriseinfo.industry],
        'enterpriseSub_legalperson': [this.ciModifyNewInfo.enterpriseinfo.legalperson],
        'enterpriseSub_license': [this.ciModifyNewInfo.enterpriseinfo.license],
      }
    );
    this.ciModifyForm.valueChanges
      .subscribe(data => this.onValueChanged(data));

    this.onValueChanged(); // (re)set validation messages now
  }

  onValueChanged(data?: any) {
    // if (!this.ciModifyForm) { return; }
    // const form = this.ciModifyForm;
    // for (const field in this.formErrors) {
    //
    //   this.formErrors[field] = '';
    //   const control = form.get(field);
    //
    //   if (control && control.dirty && !control.valid) {
    //     const messages = this.validationMessages[field];
    //     for (const key in control.errors) {
    //       this.formErrors[field] += messages[key] + ' ';
    //     }
    //   }
    // }
  }

  private getUserOriginInfo() {
    this.disableAllBtns();
    this._service.getUserOriginInfo(this.companyOrigininfoUrl)
      .then(res => {
        if (res.status === "success") {
          this.ciModifyInfo = res.data;
          this.buildForm();
        } else {
          this._errorMsg = res.data ? res.data : '网络错误';
        }
      }, error => {
        this._errorMsg = error;
      });
  }

  private getUserInfo() {
    this.disableAllBtns();
    this._service.getUserInfo(this.companyinfoUrl)
      .then(res => {
        if (res.status === "success") {
          this.ciModifyNewInfo = res.data;
          if (this.ciModifyNewInfo.enterpriseinfo.verified == '待审核') {
            this.isModifying = true;
          }
          // this.annotated();
          this.buildForm();
        } else {
          this._errorMsg = res.data ? res.data : '网络错误';
        }
      }, error => {
        this._errorMsg = error;
      });
  }

  annotated() {
    if (this.ciModifyNewInfo.enterpriseinfo.verified == "待审核" || this.ciModifyNewInfo.enterpriseinfo.verified == "未提交") {
      if (this.ciModifyInfo.enterpriseinfo.name != this.ciModifyNewInfo.enterpriseinfo.name) {
        jQuery('#newName').css('color', '#93D4E7');
      }else {
        jQuery('#newName').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.registerarea != this.ciModifyNewInfo.enterpriseinfo.registerarea) {
        jQuery('#newRegisterarea').css('color', '#93D4E7');
      }else {
        jQuery('#newRegisterarea').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.registeredcapital != this.ciModifyNewInfo.enterpriseinfo.registeredcapital) {
        jQuery('#newRegisteredcapital').css('color', '#93D4E7');
      }else {
        jQuery('#newRegisteredcapital').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.website != this.ciModifyNewInfo.enterpriseinfo.website) {
        jQuery('#newWebsite').css('color', '#93D4E7');
      }else {
        jQuery('#newWebsite').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.postcode != this.ciModifyNewInfo.enterpriseinfo.postcode) {
        jQuery('#newPostcode').css('color', '#93D4E7');
      }else {
        jQuery('#newPostcode').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.permitid != this.ciModifyNewInfo.enterpriseinfo.permitid) {
        jQuery('#newPermitid').css('color', '#93D4E7');
      }else {
        jQuery('#newPermitid').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.contact != this.ciModifyNewInfo.enterpriseinfo.contact) {
        jQuery('#newContact').css('color', '#93D4E7');
      }else {
        jQuery('#newContact').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.contacttel != this.ciModifyNewInfo.enterpriseinfo.contacttel) {
        jQuery('#newContacttel').css('color', '#93D4E7');
      }else {
        jQuery('#newContacttel').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.email != this.ciModifyNewInfo.enterpriseinfo.email) {
        jQuery('#newIds').css('color', '#93D4E7');
      }else {
        jQuery('#newIds').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.address != this.ciModifyNewInfo.enterpriseinfo.address) {
        jQuery('#newAddress').css('color', '#93D4E7');
      }else {
        jQuery('#newAddress').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.certificateid != this.ciModifyNewInfo.enterpriseinfo.certificateid) {
        jQuery('#newCertificateid').css('color', '#93D4E7');
      }else {
        jQuery('#newCertificateid').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.entertype != this.ciModifyNewInfo.enterpriseinfo.entertype) {
        jQuery('#enterpriseSub_entertype').css('color', '#93D4E7');
      }else {
        jQuery('#enterpriseSub_entertype').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.establishtime != this.ciModifyNewInfo.enterpriseinfo.establishtime) {
        jQuery('#newEstablishtime').css('color', '#93D4E7');
      }else {
        jQuery('#newEstablishtime').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.industry != this.ciModifyNewInfo.enterpriseinfo.industry) {
        jQuery('#newIndustry').css('color', '#93D4E7');
      }else {
        jQuery('#newIndustry').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.legalperson != this.ciModifyNewInfo.enterpriseinfo.legalperson) {
        jQuery('#newLegalperson').css('color', '#93D4E7');
      }else {
        jQuery('#newLegalperson').css('color', '#000000');
      }
      if (this.ciModifyInfo.enterpriseinfo.license != this.ciModifyNewInfo.enterpriseinfo.license) {
        jQuery('#newLicense').css('color', '#93D4E7');
      }else {
        jQuery('#newLicense').css('color', '#000000');
      }

    }
  }

  public addCompinfoModify(action: string): void {
    let ciInfo: any = {};
    ciInfo = Object.assign(ciInfo, {action: action, enterpriseSub_ids: this.ciModifyNewInfo.enterpriseinfo.ids});
    ciInfo = Object.assign(ciInfo, this.fileInfo);
    ciInfo = Object.assign(ciInfo, this.ciModifyForm.value);
    ciInfo = Object.assign(ciInfo, this.dateInfo);

    this._service.submit(this._ciModifyUrl, ciInfo)
      .then(res => {
        if (res.status === "success") {
          this.getUserInfo();
          if (this.ciModifyInfo.enterpriseinfo.name != ciInfo.enterpriseSub_name) {
            jQuery('#newName').css('color', '#93D4E7');
          } else {
            jQuery('#newName').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.registerarea != ciInfo.enterpriseSub_registerarea) {
            jQuery('#newRegisterarea').css('color', '#93D4E7');
          } else {
            jQuery('#newRegisterarea').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.registeredcapital != ciInfo.enterpriseSub_registeredcapital) {
            jQuery('#newRegisteredcapital').css('color', '#93D4E7');
          } else {
            jQuery('#newRegisteredcapital').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.website != ciInfo.enterpriseSub_website) {
            jQuery('#newWebsite').css('color', '#93D4E7');
          }else {
            jQuery('#newWebsite').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.postcode != ciInfo.enterpriseSub_postcode) {
            jQuery('#newPostcode').css('color', '#93D4E7');
          }else {
            jQuery('#newPostcode').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.permitid != ciInfo.enterpriseSub_permitid) {
            jQuery('#newPermitid').css('color', '#93D4E7');
          }else {
            jQuery('#newPermitid').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.contact != ciInfo.enterpriseSub_contact) {
            jQuery('#newContact').css('color', '#93D4E7');
          }else {
            jQuery('#newContact').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.contacttel != ciInfo.enterpriseSub_contacttel) {
            jQuery('#newContacttel').css('color', '#93D4E7');
          }else {
            jQuery('#newContacttel').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.email != ciInfo.enterpriseSub_email) {
            jQuery('#newIds').css('color', '#93D4E7');
          }else {
            jQuery('#newIds').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.address != ciInfo.enterpriseSub_address) {
            jQuery('#newAddress').css('color', '#93D4E7');
          }else {
            jQuery('#newAddress').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.certificateid != ciInfo.enterpriseSub_certificateid) {
            jQuery('#newCertificateid').css('color', '#93D4E7');
          }else {
            jQuery('#newCertificateid').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.entertype != ciInfo.enterpriseSub_entertype) {
            jQuery('#enterpriseSub_entertype').css('color', '#93D4E7');
          }else {
            jQuery('#enterpriseSub_entertype').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.establishtime != ciInfo.enterpriseSub_establishtime) {
            jQuery('#newEstablishtime').css('color', '#93D4E7');
          }else {
            jQuery('#newEstablishtime').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.industry != ciInfo.enterpriseSub_industry) {
            jQuery('#newIndustry').css('color', '#93D4E7');
          }else {
            jQuery('#newIndustry').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.legalperson != ciInfo.enterpriseSub_legalperson) {
            jQuery('#newLegalperson').css('color', '#93D4E7');
          }else {
            jQuery('#newLegalperson').css('color', '#000000');
          }
          if (this.ciModifyInfo.enterpriseinfo.license != ciInfo.enterpriseSub_license) {
            jQuery('#newLicense').css('color', '#93D4E7');
          }else {
            jQuery('#newLicense').css('color', '#000000');
          }

        } else {
          this._errorMsg = res.data ? res.data : '网络错误';
        }
      }, error => {
        this._errorMsg = error;
      });
  }

  onFileUploading() {
    this.disableUpload = true;
  }

  onFinishUploading(replyObj: any) {
    this.fileInfo[replyObj.property] = replyObj.rsp.data[0].ids;
    this.disableUpload = false;
  }

  private disableAllBtns() {
    this.isModifying = false;
  }

  updateDateRange(event, type: string) {
    this.dateInfo[type] = this.date2String(event.date);
  }

  private date2String(date: any): string {
    if (Object.keys(date).length === 0 || date === null || date === undefined || (date.year == 0 && date.month == 0 && date.day == 0)) {
      return null;
    }

    let str = date.year + '-';
    if (date.month < 10) {
      str += '0';
    }
    str += date.month + '-';

    if (date.day < 10) {
      str += '0';
    }
    str += date.day;
    return str;
  }

}
